package com.youku.expression.ast;

import org.springframework.expression.TypeComparator;
import org.springframework.expression.spel.support.StandardTypeComparator;

import com.youku.expression.Token;
import com.youku.expression.environment.Environment;

public abstract class OpAst extends Ast {

	private static final TypeComparator comparator = new StandardTypeComparator();

	public static TypeComparator getTypeComparator() {
		return comparator;
	}

	public OpAst(Token op, Ast exp1, Ast exp2) {
		super(op, exp1, exp2);
	}

	public Ast former() {
		return getChild(0);
	}

	public Ast latter() {
		return getChild(1);
	}

	@Override
	public Object eval(Environment environment) {
		return operate(former().eval(environment), latter().eval(environment));
	}

	protected abstract Object operate(Object left, Object right);

	@Override
	public String toString() {
		return new StringBuilder().append(former()).append(getTokenValue())
				.append(latter()).toString();
	}
}
